package com.glory.hikvisionmobile.customView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.MainThread;

import com.glory.hikvisionmobile.R;

import java.util.ArrayList;
import java.util.List;


/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/1/8 13:51
 * @DESC *****************************
 */
public class HikTabView extends LinearLayout {

    private float itemTextSize = 24;
    private int itemTextColor;
    private int itemTextGravity;
    private float itemDividerWidth;
    private float itemDividerHeight;
    private int itemDividerColor;
    private CharSequence[] itemArray;

    private Drawable divider;

    public HikTabView(Context context) {
        super(context);
        init(null, 0);
    }

    public HikTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public HikTabView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        if (attrs == null) {
            throw new IllegalArgumentException("must be init by xml");
        }
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.HikTabView, defStyle, 0);
        itemTextSize = array.getDimension(R.styleable.HikTabView_itemTextSize, 24);
        itemTextColor = array.getColor(R.styleable.HikTabView_itemTextColor, Color.parseColor("#999999"));
        itemTextGravity = array.getInt(R.styleable.HikTabView_itemTextGravity, 0);
        itemDividerWidth = array.getDimension(R.styleable.HikTabView_itemDividerWidth, 0);
        itemDividerHeight = array.getDimension(R.styleable.HikTabView_itemDividerHeight, 0);
        itemDividerColor = array.getColor(R.styleable.HikTabView_itemDividerColor, Color.parseColor("#aaaaaa"));
        itemArray = array.getTextArray(R.styleable.HikTabView_itemArray);
        array.recycle();

        divider = new DividerDrawable(itemDividerColor);
        divider.setBounds(0,0,(int) itemDividerWidth,(int) itemDividerHeight);
        setDividerDrawable(divider);

        if (itemArray != null) {
            setTabs(itemArray);
        }
    }

    @MainThread
    public void setTabs(CharSequence... tabs) {
        if (tabs != null && tabs.length > 0) {
            List<Tab> tabList = new ArrayList<>();
            for (int i = 0; i < tabs.length; i++) {
                tabList.add(new Tab(tabs[i]));
            }
            setTabs(tabList);
        }
    }

    @MainThread
    public void setTabs(Tab... tabs) {
        if (tabs != null && tabs.length > 0) {
            List<Tab> tabList = new ArrayList<>();
            for (int i = 0; i < tabs.length; i++) {
                tabList.add(tabs[i]);
            }
            setTabs(tabList);
        }
    }

    @MainThread
    public void setTabs(List<Tab> tabs) {
        removeAllViewsInLayout();
        if (tabs != null && tabs.size() > 0) {
            for (int i = 0; i < tabs.size(); i++) {
                Tab tab = tabs.get(i);
                TextView textView = new TextView(getContext());
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, itemTextSize);
                textView.setTextColor(itemTextColor);
                textView.setText(tab.name);
                if (itemTextGravity != 0) {
                    if (itemTextGravity == 1) {
                        textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
                    } else if (itemTextGravity == 3){
                        textView.setGravity(Gravity.TOP);
                    } else {
                        textView.setGravity(Gravity.CENTER);
                    }
                } else {
                    textView.setGravity(tab.gravity);
                }
                LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
                params.weight = tab.weight;
                addViewInLayout(textView, i, params);
            }
        }
        requestLayout();
    }

    public static class Tab{

        private CharSequence name;
        private int weight = 1;
        private int gravity = Gravity.CENTER_VERTICAL|Gravity.LEFT;

        public Tab(CharSequence name) {
            this.name = name;
        }

        public Tab(CharSequence name, int weight) {
            this.name = name;
            this.weight = weight;
        }

        public Tab(CharSequence name, int weight, int gravity) {
            this.name = name;
            this.weight = weight;
            this.gravity = gravity;
        }
    }
}
